package cn.meng.base.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.meng.base.service.EmployeeService;
import cn.meng.base.utils.ExcelUtil;
import cn.meng.base.vo.BusinessException;
import cn.meng.base.vo.EmployeeVo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    ExcelUtil excelUtil;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    EmployeeService employeeService;

    /**
     * parms为前端传给后端的json数据，在前端请求的data{}中
     * @param parms
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/test1")
    public String test1(@RequestBody String parms) throws BusinessException {
        //解析json，将前端传的参数转换为jsonObject
        JSONObject jsonObject = JSONUtil.parseObj(parms);
        //获取参数
        String name = (String) jsonObject.get("name");
        int age = (int) jsonObject.get("age");
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",18);
        map.put("sex","男");
        if(map.size() < 4) {
            throw new BusinessException(50,"测试自定义异常，以及异常拦截");
        }
        String result = JSON.toJSONString(map);
        return result;
    }

    /**
     * 测试无参请求，即前端的axaj中不存在data
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/test2")
    public String test2() throws BusinessException {
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("name","张三");
//        map.put("age",18);
//        map.put("sex","男");
//        if(map.size() < 4) {
//            throw new BusinessException(50,"测试自定义异常，以及异常拦截");
//        }
//        String result = JSON.toJSONString(map);
//        return result;
        return "1111";
    }

    @RequestMapping("/add")
    public String add() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("message","新增请求成功");
        return JSON.toJSONString(map);
    }

    @RequestMapping("/update")
    public String update() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("message","修改请求成功");
        return JSON.toJSONString(map);
    }

    @RequestMapping("/delete")
    public String delete() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("message","删除请求成功");
        return JSON.toJSONString(map);
    }

    @RequestMapping("/testExport1")
    public void testExport1(HttpServletResponse response) throws IOException {
        ArrayList<EmployeeVo> data = new ArrayList<>();
        EmployeeVo el = new EmployeeVo();
        el.setName("张三");
        el.setAge(20);
        el.setSex("男");
        el.setEmail("691855306@qq.com");
        el.setPosName("Java工程师");
        el.setJoblevelName("高级开发");
        el.setDepartmentName("NC Cloud供应链");
        data.add(el);

        EmployeeVo e2 = new EmployeeVo();
        e2.setName("王五");
        e2.setAge(18);
        e2.setSex("女");
        e2.setEmail("691855306@qq.com");
        e2.setPosName("前端工程师");
        e2.setJoblevelName("初级开发");
        e2.setDepartmentName("NC Cloud平台服务开发部");
        data.add(e2);

        String filename = new DateTime().toString() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);

        EasyExcel.write(response.getOutputStream(), EmployeeVo.class).sheet("模板").doWrite(data);
    }

    @RequestMapping("/testExport2")
    public void testExport2(HttpServletResponse response) throws BusinessException {
        ArrayList<EmployeeVo> data = new ArrayList<>();
        EmployeeVo el = new EmployeeVo();
        el.setName("张三");
        el.setAge(20);
        el.setSex("男");
        el.setEmail("691855306@qq.com");
        el.setPosName("Java工程师");
        el.setJoblevelName("高级开发");
        el.setDepartmentName("NC Cloud供应链");
        data.add(el);

        EmployeeVo e2 = new EmployeeVo();
        e2.setName("王五");
        e2.setAge(18);
        e2.setSex("女");
        e2.setEmail("691855306@qq.com");
        e2.setPosName("前端工程师");
        e2.setJoblevelName("初级开发");
        e2.setDepartmentName("NC Cloud平台服务开发部");
        data.add(e2);

        excelUtil.exportData(response,EmployeeVo.class,data);
    }

    @RequestMapping("/testExport3")
    public void testExport3(HttpServletResponse response) throws BusinessException {
        excelUtil.exportTemplet(response,EmployeeVo.class);
    }

    /**
     * 测试将邮件发送、公众号消息推送  通过RabbitMQ监听消息完成，通过RabbitMQ的异步并行原理优化效率
     * @return
     */
    @RequestMapping("/testMQ")
    public String testMQ() {

        /**
         * 注意：若本地测试时，需要保证下面指定的路径下 存在对应的文件
         */
        File[] files = new File[2];
        files[0] = new File("C:\\Users\\Meng\\Desktop\\测试文档1.docx");
        files[1] = new File("C:\\Users\\Meng\\Desktop\\测试文档2.docx");


        EmployeeVo el = new EmployeeVo();
        el.setName("张三");
        el.setAge(20);
        el.setSex("男");
        //可以替换成自己的QQ邮箱、或者公司邮箱去测试
        el.setEmail("691855306@qq.com");
        el.setPosName("Java工程师");
        el.setJoblevelName("高级开发");
        el.setDepartmentName("NC Cloud供应链");

        Map<String, Object> emailParams = new HashMap<>();
        emailParams.put("employee",el);
        emailParams.put("attachment",files);

        Map<String, Object> weChatParams = new HashMap<>();
        weChatParams.put("title","集成RabbitMQ测试消息推送");
        weChatParams.put("content","SpringBoot集成RabbitMQ，并将发送邮件、发送公众号消息通过RabbitMQ的异步并行形式提高效率");

        String emailExChangeName = "direct_message_exchange";
        String emailRoutingKey = "email";
        String weChatRoutingKey = "weChat";
        rabbitTemplate.convertAndSend(emailExChangeName,emailRoutingKey,emailParams);
        rabbitTemplate.convertAndSend(emailExChangeName,weChatRoutingKey,weChatParams);

        return "消息发送成功，请查收公众号消息、以及邮件消息";
    }

    @RequestMapping("/testUpdate")
    public String testUpdate() {
        employeeService.updateEmployeeNameByPk("06AB8A793E27415F8F0B","罗文康16");
        return "success";
    }

}
